package com.example.ignite.service;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;

@Data
@AllArgsConstructor
public class IgniteCacheImpl<K, V> implements Cache<K, V> {

    private Ignite ignite;

    private IgniteCache<K, V> igniteCache;

    @Override
    public V get(K key) {
        return igniteCache.get(key);
    }

    @Override
    public void put(K key, V value) {
        if (value != null && value instanceof Cache.ModifyAware) {
            ((Cache.ModifyAware) value).increaseModifyCount();
        }
        igniteCache.put(key, value);
    }

    @Override
    public boolean putIfAbsent(K key, V value) {
        if (value != null && value instanceof Cache.ModifyAware) {
            ((Cache.ModifyAware) value).increaseModifyCount();
        }
        return igniteCache.putIfAbsent(key, value);
    }

    @Override
    public void remove(K key) {
        igniteCache.remove(key);
    }

    @Override
    public <T> T doInTransaction(Callable<T> action) {
        return null;
//        Transaction tx =
//                ignite.transactions().txStart(TransactionConcurrency.PESSIMISTIC,
//                        TransactionIsolation.SERIALIZABLE);
//        try {
//            T result = action.call();
//            tx.commit();
//            return result;
//        } catch (BizException e) {
//            tx.rollback();
//            throw e;
//        } catch (Exception e) {
//            tx.rollback();
//            throw new ApplicationException("Executing in cache transaction failed", e);
//        } finally {
//            tx.close();
//        }
    }
}
